package db_main

import (
	"gorm.io/gorm"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/const"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
	pb "longmen/server/pkg/protobuf"
)

/**
 * @Author admin
 * @Description //新增一条充值记录申请
 * @version: v1.8.0
 * @Date 15:03 2022/8/6
 * @Param
 * @return
 **/

func InsertPaymentApply(req *pb.OrderNoReq, orderNo string, terminal, payChannel int) int64 {
	return global.GetX().Exec("insert into db_order(uid,order_no,type,amount,pay_amount,gold,gold_added,pay_channel,create_time,pay_status,item_carat,terminal) "+
		"select ?,?,0,price/100,price/100,gold_ios,gold_added,?,now(),0,item_carat,? from db_gold_ios_price where item_carat=?", req.UserId, orderNo, payChannel, terminal, req.ProductId).RowsAffected
}

/**
 * @Author admin
 * @Description //for and
 * @version: v1.8.0
 * @Date 17:45 2022/8/18
 * @Param
 * @return
 **/
func InsertAndroidPaymentApply(req *pb.OrderNoReq, orderNo string, terminal int, payChannel int) int64 {
	return global.GetX().Exec("insert into db_order(uid,order_no,type,amount,pay_amount,gold,gold_added,pay_channel,create_time,pay_status,item_carat,terminal) "+
		"select ?,?,0,price/100,price/100,gold,gold_added,?,now(),0,item_carat,? from db_gold_ios_price where item_carat=?", req.UserId, orderNo, payChannel, terminal, req.ProductId).RowsAffected
}

/**
 * @Author admin
 * @Description //
 * @version: v1.8.0
 * @Date 18:02 2022/8/6
 * @Param
 * @return
 **/
func UpdatePaymentApplyStatus(session *gorm.DB, productPayReq *pb.ProductPayReq) int64 {
	return session.Exec("update db_order set pay_status=1,pay_time=now(),out_trade_no=?,co_amount=amount*.7,is_manual=0 where  uid=? and item_carat=? and order_no=? and pay_status=0", productPayReq.TransactionId, productPayReq.UserId, productPayReq.ProductId, productPayReq.OrderNo).RowsAffected
}

func UpdateOtherPaymentApplyStatus(session *gorm.DB, productPayReq *pb.ProductPayReq, payStatus int) int64 {
	if payStatus == _const.PAY_SUCCESS {
		payStatus = 1
	} else {
		payStatus = 3
	}
	return session.Exec("update db_order set pay_status=?,pay_time=now(),out_trade_no=?,co_amount=amount,pay_amount=amount,amount=?,is_manual=0 where  uid=? and order_no=? and pay_status=0", payStatus, productPayReq.TransactionId, productPayReq.OrderAmt/100, productPayReq.UserId, productPayReq.OrderNo).RowsAffected
}

/**
 * @Author admin
 * @Description //返回充值申请
 * @version: v1.8.0
 * @Date 15:03 2022/8/6
 * @Param
 * @return
 **/
func GetPaymentOrderNo(userId int, orderNo string) (*pb.PaymentApply, error) {
	e := &pb.PaymentApply{}
	if err := global.GetX().Raw("select id,gold,gold gold_ios,gold_added,Convert(amount*100,SIGNED) price,item_carat,order_no,pay_status,uid user_id from db_order where uid=? and  order_no=?  and pay_status=0 ", userId, orderNo).Find(&e).Error; err != nil {
		return nil, err
	}
	return e, nil
}

/**
 * @Author admin
 * @Description //查询db_gold_ios_price
 * @version: v1.8.0
 * @Date 18:36 2022/8/9
 * @Param
 * @return
 **/
func GetGoldPricesList(order string) []*pb.GoldPrice {
	o := make([]*pb.GoldPrice, 0)
	mysql.GetRTable(&models_main.GoldIosPrice{}).Order(order).Find(&o)
	return o
}

/**
 * @Author willi
 * @Description //根据日期查询某一会员的充值记录
 * @Date 14:17 2022/9/5
 * @Param
 * @return
 **/
func GetPaymentList(startDate, endDate string, userId, page, size int32) ([]*pb.PaymentOrder, error) {
	list := make([]*pb.PaymentOrder, 0)
	if err := global.GetR().Raw("SELECT id,uid,order_no,type,vip_level,Convert(amount*100,SIGNED) amount,Convert(pay_amount*100,SIGNED) pay_amount,gold,gold_added,out_trade_no,pay_channel,date_format(create_time,'%Y-%m-%d %H:%i:%s')create_time,date_format(pay_time,'%Y-%m-%d %H:%i:%s') pay_time,"+
		"pay_status,api_id,note,is_manual,date_format(update_time,'%Y-%m-%d %H:%i:%s')update_time,process_note,item_carat,terminal,co_amount,pay_status raw_pay_status FROM `db_order` where uid=? and  create_time>=? and create_time<=? and type=0 order by create_time desc LIMIT ? OFFSET ?",
		userId, startDate, endDate, size, (page-1)*size).Find(&list).Error; err != nil {
		return nil, err
	}
	return list, nil
}

func GetPaymentListCount(startDate, endDate string, userId int32) int64 {
	var counts int64
	if err := global.GetR().Raw("select count(1) FROM `db_order` where uid=? and  create_time>=? and create_time<=? and type=0 ", userId, startDate, endDate).Scan(&counts).Error; err != nil {
		return counts
	}
	return counts
}
